Skip to content

Method: jjtAddChild(Node, int)

1: /*
2: * Copyright (c) 1997, 2018 Oracle and/or its affiliates. All rights reserved.
3: *
4: * This program and the accompanying materials are made available under the
5: * terms of the Eclipse Public License v. 2.0, which is available at
6: * http://www.eclipse.org/legal/epl-2.0.
7: *
8: * This Source Code may also be made available under the following Secondary
9: * Licenses when the conditions for such availability set forth in the
10: * Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
11: * version 2 with the GNU Classpath Exception, which is available at
12: * https://www.gnu.org/software/classpath/license.html.
13: *
14: * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
15: */
16:
17: package com.sun.el.parser;
18:
19: import jakarta.el.ELException;
20: import jakarta.el.MethodInfo;
21: import jakarta.el.PropertyNotWritableException;
22: import jakarta.el.ValueReference;
23:
24: import com.sun.el.lang.ELSupport;
25: import com.sun.el.lang.EvaluationContext;
26: import com.sun.el.util.MessageFactory;
27:
28: /**
29: * @author Jacob Hookom [jacob@hookom.net]
30: * @version $Change: 181177 $$DateTime: 2001/06/26 08:45:09 $$Author: kchung $
31: */
32: public abstract class SimpleNode extends ELSupport implements Node {
33: protected Node parent;
34:
35: protected Node[] children;
36:
37: protected int id;
38:
39: protected String image;
40:
41: public SimpleNode(int i) {
42: id = i;
43: }
44:
45: @Override
46: public void jjtOpen() {
47: }
48:
49: @Override
50: public void jjtClose() {
51: }
52:
53: @Override
54: public void jjtSetParent(Node n) {
55: parent = n;
56: }
57:
58: @Override
59: public Node jjtGetParent() {
60: return parent;
61: }
62:
63: @Override
64: public void jjtAddChild(Node n, int i) {
65:• if (children == null) {
66: children = new Node[i + 1];
67:• } else if (i >= children.length) {
68: Node c[] = new Node[i + 1];
69: System.arraycopy(children, 0, c, 0, children.length);
70: children = c;
71: }
72: children[i] = n;
73: }
74:
75: @Override
76: public Node jjtGetChild(int i) {
77: return children[i];
78: }
79:
80: @Override
81: public int jjtGetNumChildren() {
82: return (children == null) ? 0 : children.length;
83: }
84:
85: /*
86: * You can override these two methods in subclasses of SimpleNode to customize the way the node appears when the tree is
87: * dumped. If your output uses more than one line you should override toString(String), otherwise overriding toString()
88: * is probably all you need to do.
89: */
90:
91: @Override
92: public String toString() {
93: if (this.image != null) {
94: return ELParserTreeConstants.jjtNodeName[id] + "[" + this.image + "]";
95: }
96: return ELParserTreeConstants.jjtNodeName[id];
97: }
98:
99: public String toString(String prefix) {
100: return prefix + toString();
101: }
102:
103: /*
104: * Override this method if you want to customize how the node dumps out its children.
105: */
106:
107: public void dump(String prefix) {
108: System.out.println(toString(prefix));
109: if (children != null) {
110: for (int i = 0; i < children.length; ++i) {
111: SimpleNode n = (SimpleNode) children[i];
112: if (n != null) {
113: n.dump(prefix + " ");
114: }
115: }
116: }
117: }
118:
119: @Override
120: public String getImage() {
121: return image;
122: }
123:
124: public void setImage(String image) {
125: this.image = image;
126: }
127:
128: @Override
129: public Class getType(EvaluationContext ctx) throws ELException {
130: throw new UnsupportedOperationException();
131: }
132:
133: @Override
134: public Object getValue(EvaluationContext ctx) throws ELException {
135: throw new UnsupportedOperationException();
136: }
137:
138: @Override
139: public ValueReference getValueReference(EvaluationContext ctx) throws ELException {
140: return null;
141: }
142:
143: @Override
144: public boolean isReadOnly(EvaluationContext ctx) throws ELException {
145: return true;
146: }
147:
148: @Override
149: public void setValue(EvaluationContext ctx, Object value) throws ELException {
150: throw new PropertyNotWritableException(MessageFactory.get("error.syntax.set"));
151: }
152:
153: @Override
154: public void accept(NodeVisitor visitor) throws ELException {
155: visitor.visit(this);
156: if (this.children != null && this.children.length > 0) {
157: for (int i = 0; i < this.children.length; i++) {
158: this.children[i].accept(visitor);
159: }
160: }
161: }
162:
163: @Override
164: public Object invoke(EvaluationContext ctx, Class[] paramTypes, Object[] paramValues) throws ELException {
165: throw new UnsupportedOperationException();
166: }
167:
168: @Override
169: public MethodInfo getMethodInfo(EvaluationContext ctx, Class[] paramTypes) throws ELException {
170: throw new UnsupportedOperationException();
171: }
172:
173: @Override
174: public boolean equals(Object node) {
175: if (!(node instanceof SimpleNode)) {
176: return false;
177: }
178: SimpleNode n = (SimpleNode) node;
179: if (this.id != n.id) {
180: return false;
181: }
182: if (this.children == null && n.children == null) {
183: if (this.image == null) {
184: return n.image == null;
185: }
186: return this.image.equals(n.image);
187: }
188: if (this.children == null || n.children == null) {
189: // One is null and the other is non-null
190: return false;
191: }
192: if (this.children.length != n.children.length) {
193: return false;
194: }
195: if (this.children.length == 0) {
196: if (this.image == null) {
197: return n.image == null;
198: }
199: return this.image.equals(n.image);
200: }
201: for (int i = 0; i < this.children.length; i++) {
202: if (!this.children[i].equals(n.children[i])) {
203: return false;
204: }
205: }
206: return true;
207: }
208:
209: @Override
210: public boolean isParametersProvided() {
211: return false;
212: }
213:
214: @Override
215: public int hashCode() {
216: if (this.children == null || this.children.length == 0) {
217: if (this.image != null) {
218: return this.image.hashCode();
219: }
220: return this.id;
221: }
222: int h = 0;
223: for (int i = this.children.length - 1; i >= 0; i--) {
224: h = h + h + h + this.children[i].hashCode();
225: }
226: h = h + h + h + id;
227: return h;
228: }
229: }